﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace Aulac.Client.Data.Bss.PntInfos
{
    public class UnitPntAge
    {
        private const string mHourUnit = "Giờ";
        private const string mDayUnit = "Ngày";
        private const string mMonthUnit = "Tháng";
        private const string mYearUnit = "Tuổi";

        public static string HourUnit
        {
            get { return mHourUnit; }
        }
        
        public static string DayUnit
        {
            get { return mDayUnit; }
        }
        
        public static string MonthUnit
        {
            get { return mMonthUnit; }
        }
        
        public static string YearUnit
        {
            get { return mYearUnit; }
        }
        
        string mUnitAge = "";
        DateTime mBirthday = DateTime.Now;
        DateTime mCurDateTime = DateTime.Now;
        int mBirthYear = 0;
        int mAgePnt = 0;
        public string UnitAge
        {
            get { return mUnitAge; }
        }
        
        public int AgePnt
        {
            get { return mAgePnt; }
        }
        
        public int BirthYear
        {
            get { return mBirthYear; }
        }
       
        public DateTime Birthday
        {
            get { return mBirthday; }
        }
       /* 
        public UnitPntAge(DateTime pCurDateTime, int pBirthYear)
        {
            mCurDateTime  = pCurDateTime;
            mBirthYear = pBirthYear;
            if (mCurDateTime.Year - mBirthYear < 0 || pBirthYear < 1900)
                mBirthYear = mCurDateTime.Year;

            mBirthday = new DateTime(mBirthYear, 1, 1);
            CalcBirthdayPnt();
        }

        public UnitPntAge(DateTime pCurDateTime, DateTime pBirthday)
        {
            mCurDateTime = pCurDateTime;
            mBirthday = pBirthday;

            if ((pCurDateTime - pBirthday).TotalDays < 0)
                mBirthday = pCurDateTime;

            mBirthYear = mBirthday.Year;

            CalcBirthdayPnt();
        }
       
        public UnitPntAge(DateTime pCurDateTime, int pAgePnt, string pAgeUnit)
        {
            mCurDateTime = pCurDateTime;
            mAgePnt = pAgePnt;
            mUnitAge = pAgeUnit;

            if (pAgeUnit.Equals(mYearUnit))
            {
                mBirthYear = mCurDateTime.Year - mAgePnt;
                mBirthday = new DateTime(mBirthYear, 1, 1);
            }
            else if (pAgeUnit.Equals(mMonthUnit))
            {
                mBirthYear = mCurDateTime.Year;
                mBirthday = mCurDateTime.AddMonths(-mAgePnt);// new DateTime(mBirthYear, 1, 1);
                mBirthYear = mBirthday.Year;
            }

            else if (pAgeUnit.Equals(mDayUnit))
            {
                //mBirthYear = mCurDateTime.Year;
                mBirthday = mCurDateTime.AddDays(-mAgePnt);// new DateTime(mBirthYear, mCurDateTime.Month, mAgePnt);
                mBirthYear = mBirthday.Year;

            }
            else if (pAgeUnit.Equals(mHourUnit))
            {
                mBirthYear = mCurDateTime.Year;
                mBirthday = mCurDateTime.AddHours(-mAgePnt);
                mBirthYear = mBirthday.Year;
            }

        }
        */
        public UnitPntAge(DateTime pCurDateTime, int pBirthYear)
        {
            mCurDateTime = pCurDateTime;
            mBirthYear = pBirthYear;
            if (mCurDateTime.Year - mBirthYear < 0 || pBirthYear < 1900)
                mBirthYear = mCurDateTime.Year;
            mBirthday = new DateTime(mBirthYear, 1, 1);
            CalcBirthdayPnt();
        }

        public UnitPntAge(DateTime pBirthday, DateTime pCurDateTime)
        {
            mCurDateTime = pCurDateTime;
            mBirthday = pBirthday;

            if ((pCurDateTime - pBirthday).TotalDays < 0)
                mBirthday = pCurDateTime;
            mBirthYear = mBirthday.Year;
            CalcBirthdayPnt();
        }
        
        public UnitPntAge(int pAgePnt, string pAgeUnit, DateTime pCurDateTime)
        {
            mCurDateTime = pCurDateTime;
            mAgePnt = pAgePnt;
            mUnitAge = pAgeUnit;

            if (pAgeUnit.Equals(mYearUnit))
            {
                mBirthYear = mCurDateTime.Year - mAgePnt;
                mBirthday = new DateTime(mBirthYear, 1, 1);
            }
            else if (pAgeUnit.Equals(mMonthUnit))
            {
                mBirthYear = mCurDateTime.Year - (pAgePnt / 12);
                int pMonth = mCurDateTime.Month - (pAgePnt % 12);
                int pDay = mCurDateTime.Day;
                if (pMonth <= 0)
                {
                    -- mBirthYear;
                    if (pMonth == 0)
                        pMonth = 12;
                    else
                        pMonth = 12 + pMonth;// Vi pMonth < 0.                    
                }
                if (!CheckDayOfMonth(pDay, pMonth, mBirthYear))
                    pDay = DayOfMonth(pMonth, mBirthYear);
                mBirthday = new DateTime(mBirthYear, pMonth, pDay);
            }
            else if (pAgeUnit.Equals(mDayUnit))
            {
                mBirthday = ReturnToDayMonth(TotalDay(mCurDateTime.Month) - pAgePnt);
            }
            else if (pAgeUnit.Equals(mHourUnit))
            {
                mBirthYear = mCurDateTime.Year;
                mBirthday = mCurDateTime.AddHours(-mAgePnt);
                mBirthYear = mBirthday.Year;
            }
        }
        public UnitPntAge(DateTime pBirthday, int pAgePnt, string pAgeUnit, DateTime pCurDateTime)
        {
            try
            {
                mCurDateTime = pCurDateTime;
                mAgePnt = pAgePnt;
                mUnitAge = pAgeUnit;

                if (pAgeUnit.Equals(mYearUnit))
                {
                    mBirthYear = mCurDateTime.Year - mAgePnt;
                    if (mBirthYear <= 0)
                    {
                        Aulac.UtilityTools.MsgBox.Show("Tuổi không hợp lệ !", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    mBirthday = new DateTime(mBirthYear, pBirthday.Month, pBirthday.Day);
                }
                else if (pAgeUnit.Equals(mMonthUnit))
                {
                    mBirthYear = mCurDateTime.Year - (pAgePnt / 12);
                    if (mBirthYear <= 0)
                    {
                        Aulac.UtilityTools.MsgBox.Show("Tuổi không hợp lệ !", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    int pMonth = mCurDateTime.Month - (pAgePnt % 12);
                    int pDay = mCurDateTime.Day;
                    if (pMonth <= 0)
                    {
                        --mBirthYear;
                        if (pMonth == 0)
                            pMonth = 12;
                        else
                            pMonth = 12 + pMonth;// Vi pMonth < 0.                    
                    }
                    if (!CheckDayOfMonth(pDay, pMonth, mBirthYear))
                        pDay = DayOfMonth(pMonth, mBirthYear);
                    mBirthday = new DateTime(mBirthYear, pMonth, pDay);
                }
                else if (pAgeUnit.Equals(mDayUnit))
                {
                    mBirthday = ReturnToDayMonth(TotalDay(mCurDateTime.Month) - pAgePnt);
                }
                else if (pAgeUnit.Equals(mHourUnit))
                {
                    mBirthYear = mCurDateTime.Year;
                    mBirthday = mCurDateTime.AddHours(-mAgePnt);
                    mBirthYear = mBirthday.Year;
                }
            }
            catch (Exception Ex)
            {
                throw Ex;
            }            
        }
        #region Method
        private int TotalDateOfYear(int pYear)
        {
            return ((pYear % 4 == 0) && ((pYear % 100 != 0) || (pYear % 400 == 0))) ? 366 : 365;
        }
        private int DayOfMonth(int pMonth, int pYear)
        {
            int pDay = 0;
            switch (pMonth)
            {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    pDay = 31;
                    break;
                case 4:
                case 6:
                case 9:
                case 11:
                    pDay = 30;
                    break;
                case 2:
                    pDay = ((pYear % 4 == 0) && ((pYear % 100 != 0) || (pYear % 400 == 0))) ? 29 : 28;
                    break;
            }
            return pDay;
        }
        private bool CheckDayOfMonth(int pDay, int pMonth, int pYear)
        {
            bool ret = false;
            switch (pMonth)
            {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    if ((pDay <= 31) && (pDay > 0))
                        ret = true;
                    break;
                case 4:
                case 6:
                case 9:
                case 11:
                    if ((pDay <= 30) && (pDay > 0))
                        ret = true;
                    break;
                case 2:
                    int Day = ((pYear % 4 == 0) && ((pYear % 100 != 0) || (pYear % 400 == 0))) ? 29 : 28;
                    if ((pDay <= Day) && (pDay > 0))
                        ret = true;
                    break;
            }
            return ret;
        }
        private int TotalDay(int pMonth)
        {
            int pTotalDay = 0;
            for (int i = 1; i < pMonth; i++)
            {
                pTotalDay = pTotalDay + DayOfMonth(i, DateTime.Now.Year);
            }
            pTotalDay = pTotalDay + DateTime.Now.Day;
            return pTotalDay;
        }
        private DateTime ReturnToDayMonth(int pTotalDate)
        {
            DateTime pDateTime = new DateTime(DateTime.Now.Year, 1, 1);
            int pMonth = 1;
            int pDay = pTotalDate;
            int pYear = DateTime.Now.Year;
            while (pDay < 0)
            {
                --pYear;
                pDay = pDay + TotalDateOfYear(pYear);
            }
            for (int i = 1; i <= 12; i++)
            {
                if (!CheckDayOfMonth(pDay, i, DateTime.Now.Year))
                {
                    pDay = pDay - DayOfMonth(i, DateTime.Now.Year);
                    pMonth = i;
                }
                else
                    break;
            }
            if (pMonth == 12)
            {
                mBirthYear = pYear - 1;
                pDateTime = new DateTime(mBirthYear, 1, pDay);
            }
            else
            {
                mBirthYear = pYear;
                pDateTime = new DateTime(mBirthYear, pMonth + 1, pDay);
            }
            return pDateTime;
        }
        #endregion
        private void CalcBirthdayPnt()
        {
            if (Aulac.Global.Common.DateTimeHelper.YearsDifference(mCurDateTime, mBirthday) > 2)
            {
                mUnitAge = mYearUnit;
                mAgePnt = Aulac.Global.Common.DateTimeHelper.YearsDifference(mCurDateTime, mBirthday);
            }
            else if (Aulac.Global.Common.DateTimeHelper.MonthsDifference(mCurDateTime, mBirthday) >= 1)
            {
                mUnitAge = mMonthUnit;
                mAgePnt = Aulac.Global.Common.DateTimeHelper.MonthsDifference(mCurDateTime, mBirthday);
            }
            else if (Aulac.Global.Common.DateTimeHelper.DaysDifference(mCurDateTime, mBirthday) >= 1)
            {
                mUnitAge = mDayUnit;
                mAgePnt = (int)Aulac.Global.Common.DateTimeHelper.DaysDifference(mCurDateTime, mBirthday);
            }
            else if (Aulac.Global.Common.DateTimeHelper.DaysDifference(mCurDateTime, mBirthday) <= 1)
            {
                mUnitAge = mHourUnit;
                mAgePnt = 0;
            }
        }
        public static int GetTuoi(DateTime mCurDateTime, DateTime mBirthday)
        {
            try
            {
                if (Aulac.Global.Common.DateTimeHelper.YearsDifference(mCurDateTime, mBirthday) > 2)
                    return Aulac.Global.Common.DateTimeHelper.YearsDifference(mCurDateTime, mBirthday);
                else if (Aulac.Global.Common.DateTimeHelper.MonthsDifference(mCurDateTime, mBirthday) >= 1)
                    return Aulac.Global.Common.DateTimeHelper.MonthsDifference(mCurDateTime, mBirthday);
                else if (Aulac.Global.Common.DateTimeHelper.DaysDifference(mCurDateTime, mBirthday) >= 1)
                    return (int)Aulac.Global.Common.DateTimeHelper.DaysDifference(mCurDateTime, mBirthday);
                else if (Aulac.Global.Common.DateTimeHelper.DaysDifference(mCurDateTime, mBirthday) <= 1)
                    return 0;
                return 0;
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }
        public static int GetTuoi(DateTime mCurDateTime, string mBirthday)
        {
            DateTime Birthday = Convert.ToDateTime(mBirthday);
            if (Aulac.Global.Common.DateTimeHelper.YearsDifference(mCurDateTime, Birthday) > 2)
                return Aulac.Global.Common.DateTimeHelper.YearsDifference(mCurDateTime, Birthday);
            else if (Aulac.Global.Common.DateTimeHelper.MonthsDifference(mCurDateTime, Birthday) >= 1)
                return Aulac.Global.Common.DateTimeHelper.MonthsDifference(mCurDateTime, Birthday);
            else if (Aulac.Global.Common.DateTimeHelper.DaysDifference(mCurDateTime, Birthday) >= 1)
                return (int)Aulac.Global.Common.DateTimeHelper.DaysDifference(mCurDateTime, Birthday);
            else if (Aulac.Global.Common.DateTimeHelper.DaysDifference(mCurDateTime, Birthday) <= 1)
                return 0;
            return 0;
        }
    }

    public class SexRate
    {
        static string mMale = "Nam";
        static string mFemale = "Nữ";
        static string mGay = "Gay";
        public static string Male
        {
            get { return mMale; }
        }

        public static string Female
        {
            get { return mFemale; }
        }

        public static string Gay
        {
            get { return mGay; }
        }

   }

    
}
